
import java.io.*;
import java.util.HashSet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.List;
import rw.ac.kiosc.jdbc.school.School;
import rw.ac.kiosc.jdbc.school.orm.Department;
import rw.ac.kiosc.jdbc.school.orm.Student;
import rw.ac.kiosc.jdbc.school.orm.User;

public class Departments extends HttpServlet {

    private School school = new School();
    int accesses = 0;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession session = request.getSession(false);
        User user = null;
        String userIp = (String) session.getAttribute("userIp");
        int id = 0;
        String userId = (String) session.getAttribute("userId");
        if (userIp != null && userId != null) {
            try {
                id = Integer.parseInt(userId);
            } catch (Exception ex) {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
            if (userIp.equals(request.getRemoteAddr())) {
                user = school.getUserDAO().getUserById(id);
                if (user == null) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
        } else {
            session.invalidate();
            response.sendRedirect("login.htm");
        }
        displayPage(response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession session = request.getSession(false);
        User user = null;
        String userIp = (String) session.getAttribute("userIp");
        int id = 0;
        String userId = (String) session.getAttribute("userId");
        if (userIp != null && userId != null) {
            try {
                id = Integer.parseInt(userId);
            } catch (Exception ex) {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
            if (userIp.equals(request.getRemoteAddr())) {
                user = school.getUserDAO().getUserById(id);
                if (user == null) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
        } else {
            session.invalidate();
            response.sendRedirect("login.htm");
        }
        String acronym = request.getParameter("acronym");
        int yos = 0;
        try {
            yos = Integer.parseInt(request.getParameter("yos").replace(" ", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.toString());
        }
        String name = request.getParameter("name");

        Department department = new Department(acronym, yos, name, new HashSet<Student>());
        school.getDepartmentDAO().saveDepartment(department);

        displayPage(response);
    }

    private void displayPage(HttpServletResponse response) throws IOException, ServletException {
        accesses++;
        response.setContentType("text/html");
        String html = "<html>";
        html += "<head>";
        html += "<title>List of Departments</title>";
        html += "</head>";
        html += "<body>";
        html += "<h3>Welcome to KIOSC School !</h3>";
        html += "<hr>";
        html += "<ul>";
        html += "<li>";
        html += "<a href=\"welcome.html\">Home</a>";
        html += "</li>";
        html += "<li>";
        html += "<a href=\"school.xml\">School XML</a>";
        html += "</li>";
        html += "<li>";
        html += "<a href=\"students\">Students</a>";
        html += "</li>";
        html += "<li>";
        html += "<a href=\"departments\">Departments</a>";
        html += "</li>";
        html += " </ul>";
        html += "<hr>";
        html += "<h3>List of Departments</h3>";
        html += "<table border=\"1\">";
        html += "<tbody>";
        html += "<tr>";
        html += "<td>Department ID</td>";
        html += "<td>Acronym</td>";
        html += "<td>Name</td>";
        html += "<td>Number of Years</td>";
        html += "<td>Number of Students</td>";
        html += "</tr>";
        List<Department> departments = school.getDepartmentDAO().getAllDepartments();
        for (Department department : departments) {
            html += "<tr>";
            html += "<td>" + department.getDepartmentId() + "</td>";
            html += "<td>" + department.getAcronym() + "</td>";
            html += "<td>" + department.getName() + "</td>";
            html += "<td>" + department.getNumberOfYears() + "</td>";
            html += "<td>" + department.getStudents().size() + "</td>";
            html += "</tr>";
        }
        html += "<tr>";
        html += "<form method=\"post\" action=\"departments\">";
        html += "<td><input type=\"submit\" value=\"Add new\"/></td>";
        html += "<td><input type=\"text\" name=\"acronym\" id=\"acronym\"/></td>";
        html += "<td><input type=\"text\" name=\"name\" id=\"name\"/></td>";
        html += "<td><input type=\"text\" name=\"yos\" id=\"yos\"/></td>";
        html += "<td>&nbsp;</td>";
        html += "</form>";
        html += "</tr>";
        html += "</tbody>";
        html += "</table>";
        html += "</body>";
        html += "</html>";
        response.getOutputStream().write(html.getBytes());
    }
} 
